package com.infohold.plugin;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

/**
 * Created by 潜行的青衣
 */

public class AppVersion extends CordovaPlugin {

    public static final String TAG = AppVersion.class.getName();

    public boolean execute(String action, JSONArray args, CallbackContext callbackContext)
            throws JSONException {
        Activity activity = this.cordova.getActivity();
        if (action.equals("version")) {
            try {
                PackageManager manager = activity.getPackageManager();
                PackageInfo info = manager.getPackageInfo(activity.getPackageName(), 0);
                callbackContext.success(info.versionName);
                return true;
            } catch (Exception e) {
                Log.e(TAG, e.getLocalizedMessage());
            }
        }
        return false;
    }

}